// 定制协议

#pragma once

#include <iostream>
#include <string>
#include <jsoncpp/json/json.h>
#include <memory>
#include <sstream>

const std::string Sep = "\r\n";

// (json) -> len\r\n(json)\r\n  ，将消息内容封装成：长度\r\n消息\r\n
bool Encode(std::string &message) // 添加报头
{
	if (message.size() == 0)
		return false; // 报头

	// 拼接格式： 消息长度+分隔符+消息本身+分隔符
	std::string package = std::to_string(message.size()) + Sep + message + Sep; // 报文
	message = package;

	return true;
}

// len\r\n(json)\r\n ，从package中解析出JSON消息体，并放到content
bool Decode(std::string &package, std::string *content)
{
	auto pos = package.find(Sep);
	if (pos == std::string::npos)
		return false;

	// 获取消息字符串
	std::string content_length_str = package.substr(0, pos);
	int content_length = std::stoi(content_length_str);

	//	计算完整数据的总长度
	int full_length = content_length_str.size() + content_length + 2 * Sep.size();

	if (package.size() < full_length) // 数据没收全，返回
		return false;

	// 提取消息长度字符串
	*content = package.substr(pos + Sep.size(), content_length);

	// package erase ，删除已经解析的数据(避免重复解析)
	package.erase(0, full_length); //
	return true;
}

// _x oper _y
class Request
{
public:
	Request()
		: _x(0), _y(0), _oper(0) 
	{}

	Request(int x, int y, char oper)
		: _x(x), _y(y), _oper(oper)
	{
	}
	bool Serialize(std::string &out_string) // 序列化
	{
		Json::Value root;
		root["x"] = _x;
		root["y"] = _y;
		root["oper"] = _oper;

		Json::StreamWriterBuilder wb;
		std::unique_ptr<Json::StreamWriter> w(wb.newStreamWriter());
		std::stringstream ss;
		w->write(root, &ss); // 将root对象中的JSON数据写入ss字符串流
		out_string = ss.str();
		return true;
	}
	bool Deserilze(std::string &in_string) // 反序列化
	{
		Json::Value root; // 用来存储从Json字符中解析出来的数据
		Json::Reader reader;  // 读取和解析Json字符串
		bool parsingSucessful = reader.parse(in_string, root); // 解析传入的Json字符串并存储到root中
		if (!parsingSucessful)
		{
			std::cout << "Failed to parse JSON: " << reader.getFormattedErrorMessages() << std::endl;
			return false;
		}
		_x = root["x"].asInt();
		_y = root["y"].asInt();
		_oper = root["oper"].asInt();

		return true;
	}

	int X() const { return _x; }
	int Y() const { return _y; }
	char Oper() const { return _oper; }

	void Print()
	{
		std::cout << _x << std::endl;
		std::cout << _oper << std::endl;
		std::cout << _y << std::endl;
	}

private:
	int _x;
	int _y;
	char _oper;
};

class Responce
{
public:
	Responce()
		: _result(0), _code(0)
	{
	}
	Responce(int result, int code)
		: _result(result), _code(code)
	{
	}
	bool Serialize(std::string &out_string) // 序列化
	{
		Json::Value root;
		root["result"] = _result;
		root["code"] = _code;

		Json::StreamWriterBuilder wb;
		std::unique_ptr<Json::StreamWriter> w(wb.newStreamWriter());
		std::stringstream ss;
		w->write(root, &ss);
		out_string = ss.str();
		return true;
	}

	bool Deserilze(std::string &in_string) // 反序列化
	{
		Json::Value root;
		Json::Reader reader;
		bool parsingSucessful = reader.parse(in_string, root);
		if (!parsingSucessful)
		{
			std::cout << "Failed to parse JSON: " << reader.getFormattedErrorMessages() << std::endl;
			return false;
		}
		_result = root["result"].asInt();
		_code = root["code"].asInt();

		return true;
	}

	int Result() const { return _result; }
	int Code() const { return _code; };
	void SetResult(int res) {_result = res;}
	void SetCode(int c) {_code = c;} 

private:
	int _result; // 结果
	int _code;	 // 出错码 0,1，2...
};